package d0904;
import java.util.Scanner;

public class 包装器 {
    public static void main(String[] args) {

        Integer i1 = new Integer(100);
        //Integer i2 = new Integer("abcd");
        Integer i2 = new Integer("200");

        Integer i3 = Integer.valueOf(300); // 返回 Ineger
        Integer i3_1 = Integer.valueOf("300"); // 返回 Ineger
        int i4 = Integer.parseInt("400"); // 返回 int

        // 自动拆箱
        int i5 = Integer.valueOf(500);
        // 转换
        Integer i5_1 = Integer.valueOf(500);
        // 自动拆箱
        int i5_2 = i5_1;
        // 自动装箱
        Integer i6 = 100;

        // 包装器类型的缓存特性
        Integer a = Integer.valueOf(-128);
        Integer b = Integer.valueOf(-128);
        System.out.println(a == b); // 因为 a 和 b 是同一个对象

        a = Integer.valueOf(128);
        b = Integer.valueOf(128);
        System.out.println(a == b); // 因为 a 和 b 是2个完全不同的对象

        // -128~127 对 256 个数字进行缓存

        a = new Integer(100); // new 不会到缓存冲取值
        b = new Integer(100);
        System.out.println(a == b); // false

        System.out.println(a.intValue() == b.intValue()); // true
        System.out.println(a.equals(b)); // true  比较内容

        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.print("请输入学号");
        String line = sc.nextLine();
        s.setId(Integer.valueOf(line));
        System.out.println("学号==>" + s.getId());

    }

}

class Student {
    //录入以下属性学生信息
    private int id; //学号
    private String name; // 姓名
    private char gender; // 性别
    private boolean threeGood; // 三好学生
    private float height; // 身高
    private String phone; // 电话

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", threeGood=" + threeGood + ", height="
                + height + ", phone=" + phone + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + gender;
        result = prime * result + Float.floatToIntBits(height);
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((phone == null) ? 0 : phone.hashCode());
        result = prime * result + (threeGood ? 1231 : 1237);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (gender != other.gender)
            return false;
        if (Float.floatToIntBits(height) != Float.floatToIntBits(other.height))
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (phone == null) {
            if (other.phone != null)
                return false;
        } else if (!phone.equals(other.phone))
            return false;
        if (threeGood != other.threeGood)
            return false;
        return true;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public boolean isThreeGood() {
        return threeGood;
    }

    public void setThreeGood(boolean threeGood) {
        this.threeGood = threeGood;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}
